﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections.Generic;
// AnyCAD
using AnyCAD.Platform.Core;
using AnyCAD.Platform.Geometry;
using AnyCAD.Presentation;


namespace AnyCADPresentation
{
    public partial class PresentationForm : Form
    {
        public AnyCAD.Presentation.Application m_Application;
        public View3d m_View3d;
        public Document3d m_Document;
        public int m_nCurUserId;

        public int m_nCommand;
        public bool m_bOnMoving;

        public PresentationForm()
        {
            InitializeComponent();
            this.MouseWheel += new System.Windows.Forms.MouseEventHandler(this.View_MouseWheel);
            m_nCurUserId = 0;
            m_bOnMoving = false;
        }

        public void ShowMessage(String szMessage)
        {
            toolStripStatusLabel1.Text = szMessage;
        }

        private void Form_Load(object sender, EventArgs e)
        {
            m_Application = new AnyCAD.Presentation.Application();
            m_Application.Initialize();

            m_Document = new Document3d(m_Application);
            m_View3d = new View3d(m_Document);

            m_Document.EntityListener = new MyEntityNodeListener(this);

            m_View3d.Initialize(this.Handle);
            m_View3d.Resize(this.Size.Width, this.Size.Height);

            // Set Draw Plane Cell
            DrawPlane drawPlane = new DrawPlane();
            drawPlane.CellSize = 100;
            drawPlane.CountX = 20;
            drawPlane.CountY = 18;
            m_View3d.SetDrawPlane(drawPlane);

            TextNode textNode = new TextNode();
            textNode.SetText("Hello AnyCAD");
            textNode.SetScale(new Vector3(20, 20, 20));
            m_Document.AddTextNode(textNode);

            ScriptManager scriptManager = new ScriptManager();
            scriptManager.AddScript("Hello", "AnyCAD.MessageBox(\"Title\", \"I'm from AnyCAD.Python\")");
            scriptManager.Run("Hello", null, null);

            // Set Active View
            AnyCAD.Presentation.Application.SetActiveView(m_View3d);
        }

        private void Form_Closing(object sender, FormClosingEventArgs e)
        {
            m_View3d.Destroy();

            m_Application.Destroy();
        }

        private void View_Resize(object sender, EventArgs e)
        {
            try
            {
                m_View3d.Resize(this.Size.Width, this.Size.Height);
                m_View3d.UpdateView();
            }
            catch
            {

            }

        }

        private void View_MouseDown(object sender, MouseEventArgs e)
        {
            m_View3d.OnMouseDown(e);

            m_bOnMoving = false;
            // Select
            if (m_nCommand == 1)
            {
                List<EntityNode> listNodes = m_View3d.GetSelectedEntities();
                if (listNodes.Count == 1)
                {
                    DialogResult result = MessageBox.Show(listNodes[0].GetName(), "Selected");
                }
            }
            // Move
            else if (m_nCommand == 2)
            {
                List<EntityNode> listNodes = m_View3d.GetSelectedEntities();
                if (listNodes.Count == 1)
                {
                    m_bOnMoving = true;
                }
            }
        }

        private void View_MouseMove(object sender, MouseEventArgs e)
        {
            m_View3d.OnMouseMove(e);
        }

        private void View_MouseUp(object sender, MouseEventArgs e)
        {
            if (m_nCommand == 2 && m_bOnMoving)
            {
                List<EntityNode> listNodes = m_View3d.GetSelectedEntities();
                if (listNodes.Count == 1)
                {
                    EntityNode node = listNodes[0];
                    DialogResult result = MessageBox.Show(node.GetName(), "Moved");
                    Vector3 newPos = node.GetPosition();
                }

            }
            m_View3d.OnMouseUp(e);


        }

        private void View_MouseWheel(object sender, MouseEventArgs e)
        {
            m_View3d.OnMouseWheel(e);
        }

        private void View_Paint(object sender, PaintEventArgs e)
        {
            m_View3d.UpdateView();
        }

        private void dToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Filter = "3DS files (*.3ds)|*.3ds";
            if (DialogResult.OK == dlg.ShowDialog())
            {
                EntityNodeFactory factory = new EntityNodeFactory(m_View3d);
                EntityNode entityNode = factory.Import3DS(dlg.FileName);
                entityNode.SetUserId(++m_nCurUserId);
                entityNode.SetSize(new Vector3(100, 100, 100));
                entityNode.SetPosition(new Vector3(100, 100, 0));
                entityNode.SetFaceColor(new ColorValue(1, 0, 0, 1));
                m_View3d.UpdateView();
            }
        }

        private void lineToolStripMenuItem_Click(object sender, EventArgs e)
        {
            m_Application.ExecuteCommand(m_View3d, "Line");
        }

        private void arcToolStripMenuItem_Click(object sender, EventArgs e)
        {
            m_Application.ExecuteCommand(m_View3d, "Arc");
        }

        private void circleToolStripMenuItem_Click(object sender, EventArgs e)
        {
            m_Application.ExecuteCommand(m_View3d, "Circle");
        }

        private void rectangleToolStripMenuItem_Click(object sender, EventArgs e)
        {
            m_Application.ExecuteCommand(m_View3d, "Rectangle");
        }

        private void splineToolStripMenuItem_Click(object sender, EventArgs e)
        {
            m_Application.ExecuteCommand(m_View3d, "Spline");
        }

        private void rotateToolStripMenuItem_Click(object sender, EventArgs e)
        {
            m_Application.ExecuteCommand(m_View3d, "RotateCamera");
        }

        private void fitAllToolStripMenuItem_Click(object sender, EventArgs e)
        {
            m_Application.ExecuteCommand(m_View3d, "FitAll");
        }

        private void selectToolStripMenuItem_Click(object sender, EventArgs e)
        {
            m_Application.ExecuteCommand(m_View3d, "Pick");
            m_nCommand = 1;
        }

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveFileDialog dlg = new SaveFileDialog();
            dlg.Filter = "AnyCAD files (*.axml)|*.axml" ;

            if( DialogResult.OK == dlg.ShowDialog())
            {
                m_Document.SaveDocument(dlg.FileName);
            }
        }

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Filter = "AnyCAD files (*.axml)|*.axml";

            if (DialogResult.OK == dlg.ShowDialog())
            {
                m_Document.OpenDocument(dlg.FileName);
                m_View3d.UpdateView();
            }
        }

        private void componentToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Filter = "AnyCAD files (*.cxml)|*.cxml";

            if (DialogResult.OK == dlg.ShowDialog())
            {
                EntityNodeFactory factory = new EntityNodeFactory(m_View3d);
                EntityNode entityNode = factory.ImportComponent(dlg.FileName);
                entityNode.GetName();
                m_View3d.UpdateView();
            }       
        }

        private void import1000TimesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            EntityNodeFactory factory = new EntityNodeFactory(m_View3d);
            EntityNode entityNode = factory.ImportComponent("C:\\model3\\trans.cxml");

            for (int ii = 0; ii < 10; ++ii)
                for (int jj = 0; jj < 100; ++jj)
                {
                    EntityNode pNewNode = factory.ImportComponent("C:\\model3\\trans.cxml");
                    Vector3 pos = new Vector3();
                    pos.X = ii * 100;
                    pos.Y = jj * 100;
                    pNewNode.SetPosition(pos);
                }

            m_View3d.UpdateView();
        }

        private void spline2SegmentsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            List<Vector3> spline = new List<Vector3>();
            spline.Add(new Vector3(0, 0, 0));
            spline.Add(new Vector3(10, 10, 10));
            spline.Add(new Vector3(100, 100, 200));
            List<Vector3> segments = TopoTools.SplineToSegments(spline, 10);
            int nCount = segments.Count;
        }

        private void moveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            m_Application.ExecuteCommand(m_View3d, "Move");
            m_nCommand = 2;
        }

        
    }
}
